﻿@model Serene.Navigation.NavigationModel

@helper renderItem(Serenity.Navigation.NavigationItem item, int depth, int[] path) {

var isactive = true;
for (var i = 0; i <= depth; i++)
{
    if (path[i] != Model.ActivePath[i])
    {
        isactive = false;
        break;
    }
}

var klass = (isactive ? "active" : "") + (item.Children.IsEmptyOrNull() ? "" : " treeview");
if (!string.IsNullOrEmpty(item.ItemClass))
{
    klass = (klass + " " + item.ItemClass).Trim();
}
var icon = (item.IconClass ?? (item.Children.Count > 0 ? "icon-layers" : (depth == 0 ? "icon-link" : "fa-circle-o")));
var title = (LocalText.TryGet("Navigation." + item.FullPath) ?? item.Title);
var sectionUrl = (item.Url ?? "javascript:;");
var target = item.Target; if (target != null) { target += " target=" + target; }

<li class="@klass">
    <a href="@sectionUrl"@target>
        <i class="nav-icon fa @(icon)"></i>
        <span>@title</span>
        @if (!item.Children.IsEmptyOrNull()) {
        <i class="fa fa-angle-left pull-right"></i>
        }
    </a>

    @if (item.Children.Count > 0)
    {
        <ul class="treeview-menu">
        @{path[depth + 1] = 0; }
        @foreach (var child in item.Children) {
            @renderItem(child, depth + 1, path);
        }
        </ul>
    }
</li>
path[depth]++;
}

@{var path = new int[10]; }
@foreach (var item in Model.Items)
{
    @renderItem(item, 0, path);
}

<script type="text/javascript">
    function openWindow(url, width, height) {
        height = height || (screen.availHeight - 60);
        width = width || (screen.availWidth - 10);
        var x = (screen.availWidth - width) / 2 - 5; if (x < 0) x = 0;
        var y = (screen.availHeight - height) / 2 - 25; if (y < 0) y = 0;
        var winPopup = window.open(url, "", "status=0, toolbar=0, width=" + width + ", height=" + height + 
            ", scrollbars=1, resizable=yes, left=" + x + ", top=" + y);
    }
</script>